package com.fos.BluetoothStory.adapter;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fos.BluetoothStory.R;
import com.fos.BluetoothStory.activity.MainActivity;
import com.fos.BluetoothStory.data.TFMusicState;
import com.fos.BluetoothStory.model.MusicBean;
import com.fos.BluetoothStory.service.PlaySongService;
import com.fos.BluetoothStory.util.AppConstance;

public class SongsListAdapter extends RecyclerView.Adapter<SongsListAdapter.ViewHolder>
	implements OnClickListener{
	public static class ViewHolder extends RecyclerView.ViewHolder{
		public TextView mTextView; 
		public ViewHolder(View itemView) {
			super(itemView);
			mTextView = (TextView)itemView.findViewById(R.id.tv_name);
		}
		
	}
	
	private Context mContext;
	private ArrayList<MusicBean> mValues;
	private ArrayList<MusicBean> srcValues;
	private LayoutInflater inflater;
	private int lastSelectedP = -1;
//	private boolean isPlaying;
	
	/**上下事件*/
	private BroadcastReceiver nextReceiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			int pa = intent.getIntExtra(PlaySongService.NAME_ACTION, PlaySongService.NAME_ACTION_NEXT);
			if(getItemCount() > 0){
				switch (pa) {
					case PlaySongService.NAME_ACTION_NEXT:
						lastSelectedP++;
						if(lastSelectedP > getItemCount()-1){
							lastSelectedP = 0;
						}
						break;
					case PlaySongService.NAME_ACTION_UP:
						lastSelectedP--;
						if(lastSelectedP < 0){
							lastSelectedP = getItemCount()-1;
						}
						break;
				}
				sendPlay(lastSelectedP);
				notifyDataSetChanged();
			}
		}
	};
	
	public SongsListAdapter(Context ctx) {
		mContext = ctx;
		inflater = LayoutInflater.from(ctx);
		LocalBroadcastManager.getInstance(mContext).registerReceiver(nextReceiver, new IntentFilter(PlaySongService.INTENT_PLAY_ACTION));
	}
	
	public void setDatas(ArrayList<MusicBean> datas, String playingSong){
		MusicBean playMusicBean = TFMusicState.getInstance().getPlayMusic();
				
		if(mValues != null){
			mValues.clear();
		}else{
			mValues = new ArrayList<MusicBean>();
		}
		lastSelectedP = -1;
		if(datas!=null && datas.size()>0){
			for(int i=0; i<datas.size(); i++){
				MusicBean info = datas.get(i);
				mValues.add(info);
				if(AppConstance.sPID == -1){//本地的
					if(!TextUtils.isEmpty(playingSong) && playingSong.equals(info.url)){
						lastSelectedP = i;
					}
				}else{//设备的
					if(playMusicBean!=null && playMusicBean.equals(info)){
						lastSelectedP = i;
					}
				}
			}
		}
//		if(TextUtils.isEmpty(playingSong)){
//			lastSelectedP = 0;
//		}
		srcValues = mValues;
		notifyDataSetChanged();
	}

	@Override
	public int getItemCount() {
		return mValues==null ? 0 : mValues.size();
	}

	@Override
	public void onBindViewHolder(ViewHolder holder, int position) {
		MusicBean songInfo = mValues.get(position);
		holder.mTextView.setText(songInfo.title);
		holder.mTextView.setTag(position);
		holder.mTextView.setOnClickListener(this);
		holder.mTextView.setSelected(lastSelectedP==position);
	}

	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View itemView = inflater.inflate(R.layout.adapter_item_song, parent, false);
		ViewHolder holder = new ViewHolder(itemView);
		return holder;
	}

	@Override
	public void onClick(View v) {
		int p = (int)v.getTag();
		setSelected(p);
		
		sendPlay(p);
	}

	public void setSelected(int p) {
		if(lastSelectedP != p){//改变item样式
			lastSelectedP = p;
			notifyDataSetChanged();
		}
	}

	//播放音乐
	private void sendPlay(int p) {
		if(AppConstance.sPID < 0){//播放本地
			if(p>=0 && p<getItemCount()){
				Intent service = new Intent(mContext, PlaySongService.class);
				mContext.startService(service);
				
				Intent songB = new Intent(PlaySongService.INTENT_PLAY_SONG);
				songB.putExtra(PlaySongService.BROAD_SONG, mValues.get(p).url);
				LocalBroadcastManager.getInstance(mContext).sendBroadcast(songB);
			}
		}
		((MainActivity)mContext).playBlueMusic();
	}
	
	public MusicBean getPlayItem(){
		if(lastSelectedP<0 || lastSelectedP>=getItemCount())
			return null;
		return mValues.get(lastSelectedP);
	}
	
	/**
	 * 响应搜索事件
	 * @param text
	 * @author FAN 创建于Dec 29, 2015
	 */
	public void doSearch(CharSequence text) {
		if(TextUtils.isEmpty(text) && (srcValues==null || srcValues.size()==0)){//首次搜索空的
			return;
		}
		
		if(TextUtils.isEmpty(text)){
			mValues = srcValues;
		}else{
			String ts = text.toString().toUpperCase();
			ArrayList<MusicBean> newValues = new ArrayList<MusicBean>();
			for(MusicBean bean : srcValues){
				if(bean.title.toUpperCase().contains(ts)){
					newValues.add(bean);
				}
			}
			mValues = newValues;
		}
		notifyDataSetChanged();
	}
}
